import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import AutoDateLocator, DateFormatter

# commit耗时画图
df = pd.read_csv('commit.csv', parse_dates=['timestamp'])

total_time = len(df.commit)
interval_time = [0, 0, 0, 0]
for commit_delay in df.commit:
    if commit_delay < 5:
        interval_time[0] = interval_time[0] + 1
    elif commit_delay < 10:
        interval_time[1] = interval_time[1] + 1
    elif commit_delay < 20:
        interval_time[2] = interval_time[2] + 1
    else:
        interval_time[3] = interval_time[3] + 1
print("0-5 ms比例[{:d}/{:d}]:{:2.1f}%".
      format(interval_time[0], total_time, 
             interval_time[0] / total_time * 100))
print("5-10ms比例[{:d}/{:d}]:{:2.1f}%".
      format(interval_time[1], total_time, 
             interval_time[1] / total_time * 100))
print("10-20ms比例[{:d}/{:d}]:{:2.1f}%".
      format(interval_time[2], total_time, 
             interval_time[2] / total_time * 100))
print(">20ms比例[{:d}/{:d}]:{:2.1f}%".
      format(interval_time[3], total_time, 
             interval_time[3] / total_time * 100))

plt.figure(figsize=(6.5, 8))
plt.plot_date(df.timestamp, df.commit, fmt='b.', markersize=1)
ax = plt.gca()
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
ax.xaxis.set_major_locator(AutoDateLocator(maxticks=30))
plt.xticks(rotation=90, ha='center')
plt.grid()
plt.ylim(0, 80)
ax.set_title(u'commit耗时波动情况', fontproperties='SimHei', fontsize=14)
ax.set_xlabel('timestamp')
ax.set_ylabel('commit delay (ms)')

# tps波动画图
df = pd.read_csv('tps.csv', parse_dates=['timestamp'])

plt.figure(figsize=(6.5, 8))
plt.plot_date(df.timestamp, df.tps, fmt='b.')
ax = plt.gca()
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
ax.xaxis.set_major_locator(AutoDateLocator(maxticks=30))
plt.xticks(rotation=90, ha='center')
plt.grid()
ax.set_title(u'tps波动情况', fontproperties='SimHei', fontsize=14)
ax.set_xlabel('timestamp')
ax.set_ylabel('tps')

plt.show()
